package com.laikan.reader.article.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.laikan.reader.R;
import com.laikan.reader.core.bean.FocusReplyDetailBean;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/11/8.
 */
public class FocusReplyAdapter extends BaseAdapter {

    protected Context mContext;
    protected LayoutInflater mInflater;
    protected ArrayList<FocusReplyDetailBean.ReplyListData> datas;

    public FocusReplyAdapter(Context context, ArrayList<FocusReplyDetailBean.ReplyListData> datas) {
        mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.datas = datas;
    }

    public void cleanDatas() {
        if (datas != null && !datas.isEmpty()) {
            datas.clear();
            notifyDataSetChanged();
        }
    }

    public ArrayList<FocusReplyDetailBean.ReplyListData> getDatas() {
        return this.datas;
    }

    public void setDatas(ArrayList<FocusReplyDetailBean.ReplyListData> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void addDatas(ArrayList<FocusReplyDetailBean.ReplyListData> datas) {
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getCount() {
        if (null != datas) {
            return datas.size();
        }
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.lk_reply_item, null);
            holder.person_img = (com.facebook.drawee.view.SimpleDraweeView)convertView.findViewById(R.id.head_reviewer_img);
            holder.username = (TextView)convertView.findViewById(R.id.head_reviewer_name);
            holder.reply_time = (TextView)convertView.findViewById(R.id.reply_time);
            holder.reply_content = (TextView)convertView.findViewById(R.id.reply_content);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }
        //               KLog.d("TAG", "tupian = " + datas.get(position).getReplyUserIcon() + "-----------------------------");
        if (null != (datas.get(position).getReplyPic())) {
            holder.person_img.setImageURI(Uri.parse(datas.get(position).getReplyPic()));
        }
        if (null != (datas.get(position).getReplyName())) {
            holder.username.setText(datas.get(position).getReplyName());
        }
        if (null != (datas.get(position).getCreateTime())) {
            //                    SimpleDateFormat sf = new SimpleDateFormat("MM-dd HH:mm");
            holder.reply_time.setText(datas.get(position).getCreateTime());
            //            holder.reply_time.setText(PublicUtil.timeStamp2DateFormatString(datas.get(position).getReplyCreateTime(), "MM-dd HH:mm"));
        }
        if (null != (datas.get(position).getRepContent())) {
            holder.reply_content.setText(datas.get(position).getRepContent());
        }
        return convertView;
    }

    private class ViewHolder {

        private com.facebook.drawee.view.SimpleDraweeView person_img;
        private TextView username, reply_time, reply_content;
    }

}
